package com.yunche.yunchebusiness.ui.activity

import android.content.Intent
import android.net.Uri
import android.support.v4.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.mvp.contract.UpdateContract
import com.yunche.yunchebusiness.mvp.presenter.UpdatePresenter
import com.yunche.yunchebusiness.ui.fragment.AccountBookFragment
import com.yunche.yunchebusiness.ui.fragment.EvaluateFragment
import com.yunche.yunchebusiness.ui.fragment.HomeFragment
import com.yunche.yunchebusiness.ui.fragment.SystemFragment
import com.yunche.yunchebusiness.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseActivity(), UpdateContract.View {

    private var currentIndex = 0
    private var fragments = arrayListOf<Fragment>()
    override fun setLayout(): Int = R.layout.activity_main

    private val mPresenter by lazy {
        UpdatePresenter()
    }

    override fun initView() {
        mPresenter.attachView(this)
        fragments.add(HomeFragment.getInstance())
        fragments.add(AccountBookFragment.getInstance())
        fragments.add(EvaluateFragment())
        fragments.add(SystemFragment.getInstance())

        bottom_navigation.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.navigation_home -> {
                    switchFragment(0)
                }
                R.id.navigation_book -> {
                    switchFragment(1)
                }
                R.id.navigation_evaluate -> {
                    switchFragment(2)
                }
                R.id.navigation_system -> {
                    switchFragment(3)
                }
            }
            true
        }

        supportFragmentManager.beginTransaction().add(R.id.frame_content, fragments[currentIndex])
            .commitAllowingStateLoss()
    }


    override fun noNewVersion() {
        // ToastyUtils.showInfo("没有新版本")

    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showError(msg: String) {

    }
    override fun hasNewVersion(info: String, downloadUrl: String, isMust: Boolean) {
        MaterialDialog.Builder(mContext!!)
            .title("发现新版本")
            .content(info)
            .positiveText("去更新")
            .onPositive { dialog, which ->
                val uri = Uri.parse(downloadUrl)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                startActivity(intent)
            }
            .cancelable(isMust)
            .show()
    }



    private fun switchFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        if (currentIndex == index) {
            return
        }
        if (fragments[index].isAdded) {
            transaction.hide(fragments[currentIndex]).show(fragments[index]).commitAllowingStateLoss()
        } else {
            transaction.hide(fragments[currentIndex]).add(R.id.frame_content, fragments[index])
                .commitAllowingStateLoss()
        }
        currentIndex = index
    }

    override fun onBackPressed() {
        exitApp()
    }

    override fun initData() {
        mPresenter.checkVersion(CommonUtils.getVersionName(mContext!!), 4)
    }

    override fun initEvent() {
    }
}
